package com.example.service.impl;

import com.example.entity.FacilityClassify;
import com.example.entity.Page.MyPageHelper;
import com.example.entity.Page.PageRequest;
import com.example.mapper.FacilityClassifyMapper;
import com.example.result.CommonResult;
import com.example.service.FacilityClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: tobed
 * @date: 2022/11/14
 * @time: 10:14
 */
@Service
public class FacilityClassifyServiceImpl implements FacilityClassifyService {

    @Autowired
    FacilityClassifyMapper facilityClassifyMapper;

    /*设备分类全查*/
    @Override
    public CommonResult list(String facilityClassifyName, PageRequest pageRequest) {
        MyPageHelper.startPage(pageRequest);
        List<FacilityClassify> list = facilityClassifyMapper.list(facilityClassifyName);
        return CommonResult.ok(MyPageHelper.getPageResult(list));
    }

    /*状态修改*/
    @Override
    public CommonResult updateStatus(Integer id,int status) {
        if (status==0){
            status = 0;
        }else{
            status = 1;
        }
        Date updateTime = new Date();
        int i = facilityClassifyMapper.updateStatus(updateTime,status,id);
        if (i!=0){
            return CommonResult.ok();
        }else{
            return CommonResult.fail(888,"网络异常");
        }
    }

    /*修改*/
    @Override
    public CommonResult update(Integer id, String facilityClassifyName) {
        int i = facilityClassifyMapper.update(id,facilityClassifyName,new Date());
        if (i!=0){
            return CommonResult.ok();
        }else{
            return CommonResult.fail(888,"网络异常");
        }
    }

    /*删除分类*/
    @Override
    public CommonResult remove(Integer id) {
        int i = facilityClassifyMapper.remove(id);
        if (i!=0){
            return CommonResult.ok();
        }else{
            return CommonResult.fail(888,"网络异常");
        }
    }

    /*添加分类信息*/
    @Override
    public CommonResult create(FacilityClassify facilityClassify) {
        facilityClassify.setCreateTime(new Date());
        facilityClassify.setStatus(0);
        int i = facilityClassifyMapper.create(facilityClassify);
        if (i!=0){
            return CommonResult.ok();
        }else{
            return CommonResult.fail(888,"网络异常");
        }
    }
}
